package model

import (
	"github.com/jmoiron/sqlx"
	"time"
	"zimuzu_cvn_web_api/config"
	. "zimuzu_cvn_web_api/pkg/e"
)

type CvnFrozenDB struct {
	ID           int64     `db:"id" json:"id,omitempty"`
	UID          int64     `db:"uid" json:"uid,omitempty"`
	Amount       float64   `db:"amount" json:"amount,omitempty"`
	FrozenAmount float64   `db:"frozen_amount" json:"frozen_amount,omitempty"`
	Source       int       `db:"source" json:"source,omitempty"`
	Status       int       `db:"status" json:"status,omitempty"`
	CreateTime   int64     `db:"create_time" json:"create_time,omitempty"`
	UpdateTime   time.Time `db:"update_time" json:"update_time,omitempty"`
}

//查询cvn_frozen 用户对应类型的冻结资产详情
//Auth:Xu
func GetCvnFrozenInfo(uid int64, frozenType int) (userFrozen CvnFrozenDB, err error) {
	err = sqlx.Get(config.C.DB.DB, &userFrozen, "SELECT amount,frozen_amount,status, update_time FROM cvn_frozen WHERE uid = ? and `source` = ?", uid, frozenType)
	if err != nil {
		Log("err", err.Error())
	}
	return
}

//插入cvn_frozen 对应类型的冻结资产记录
//Auth:Xu
func InsertCvnFrozen(db sqlx.Execer, userFrozen CvnFrozenDB) (err error) {
	_, err = db.Exec("INSERT INTO cvn_frozen (uid,amount,frozen_amount,source,create_time ) VALUES (?,?,?,?,?)", userFrozen.UID, userFrozen.Amount, userFrozen.FrozenAmount, userFrozen.Source, userFrozen.CreateTime)
	if err != nil {
		Log("err", err.Error())
	}
	return err
}

//插入cvn_frozen 冻结资产详情
//Auth:Xu
func UpdateCvnFrozenAmount(db sqlx.Execer, amount float64, frozenAmount float64, uid int64, source int) (err error) {
	_, err = db.Exec("UPDATE `cvn_frozen` SET `amount` = ?,`frozen_amount` = ?  WHERE `uid` = ? and source = ? ",
		amount, frozenAmount, uid, source)
	if err != nil {
		Log("err", uid, amount, err.Error())
	}
	return
}

// 查询cvn_frozen 全网对应类型的冻结资产
// Auth:Xu
func GetGlobalCvnFrozen(frozenType int) (total float64, err error) {
	err = sqlx.Get(config.C.DB.DB, &total, "SELECT IFNULL(SUM(frozen_amount), 0.00) FROM cvn_frozen "+
		" WHERE `source` = ?", frozenType)
	if err != nil {
		Log("err", err.Error())
	}
	return
}
